﻿using System;
using System.Windows.Forms;

namespace Inaction.UI
{
    public partial class ChangePasswordForm : Form
    {
        public ChangePasswordForm()
        {
            InitializeComponent();
            TopLevel = false;
            tb_UserName.Text = Inaction.Security.Principal.Instance.Identity.Name;
            AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange;
        }
        private void btn_Confirm_Click(object sender, EventArgs e)
        {
            var hasError = false;

            #region 错误检查
            if (string.IsNullOrEmpty(tb_Password.Text))
            {
                errorProvider.SetError(tb_Password, "原密码输入不能为空");
                hasError = true;
            }
            else if (tb_Password.Text.Length < 3)
            {
                errorProvider.SetError(tb_Password, "字符长度为3~10");
                hasError = true;
            }
            if (string.IsNullOrEmpty(tb_NewPassword.Text))
            {
                errorProvider.SetError(tb_NewPassword, "新密码输入不能为空");
                hasError = true;
            }
            else if (tb_NewPassword.Text.Length < 3)
            {
                errorProvider.SetError(tb_NewPassword, "字符长度为3~10");
                hasError = true;
            }
            if (string.IsNullOrEmpty(tb_ConfirmPassword.Text))
            {
                errorProvider.SetError(tb_ConfirmPassword, "确认密码输入不能为空");
                hasError = true;
            }
            else if (tb_ConfirmPassword.Text.Length < 3)
            {
                errorProvider.SetError(tb_ConfirmPassword, "字符长度为3~10");
                hasError = true;
            }
            if (tb_NewPassword.Text != tb_ConfirmPassword.Text)
            {
                errorProvider.SetError(tb_Password, "两次密码输入不一致");
                hasError = true;
            }
            if (tb_Password.Text == tb_NewPassword.Text)
            {
                errorProvider.SetError(tb_NewPassword, "新密码与原密码一致");
                hasError = true;
            }
            #endregion

            if (hasError) return;
            var userNumber = Inaction.Security.Principal.Instance.Identity.UserNumber;
            var password = tb_Password.Text;
            var newPassword = tb_NewPassword.Text;
            Func<string, string, string, string> method = Inaction.Security.Principal.ChangePassword;
            var result = RemotingMethod.Invoke<string>(method, userNumber, password, newPassword);

            if (string.IsNullOrEmpty(result))
            {
                MessageBox.Show("修改成功,请退出系统重新登陆一次");
                Close();
            }
            else
            {
                MessageBox.Show(result);
            }
        }
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tb_ConfirmPassword_Validated(object sender, EventArgs e)
        {
            var textBox = sender as TextBox;
            if (string.IsNullOrEmpty(textBox.Text))
            {
                errorProvider.SetError(textBox, "值不能为空");
            }
            else if (textBox.Text.Length < 3)
            {
                errorProvider.SetError(textBox, "字符长度为3~10");
            }
            else errorProvider.SetError(textBox, string.Empty);
        }
    }
}
